import Dialog from './dialog.vue'
import Upload from '../upload'
import DialogNoFooter from './dialog-nofooter.vue'

// 定义插件对象
const MessageBox = {}
// vue的install方法，用于定义vue插件
MessageBox.install = function (Vue, options) {
  const MessageBoxInstance = Vue.extend(Dialog)
  let currentMsg
  const initInstance = ({ content, parent, params, ok, cancel }) => {
    // 实例化vue实例
    currentMsg = new MessageBoxInstance({
      mixins: [{
        methods: {
          okx (vm) {
            if (typeof ok === 'function') {
              ok(vm.$refs.mldContent)
            }
          },
          cancel (vm) {
            if (typeof cancel === 'function') {
              cancel(vm.$refs.mldContent)
            }
          }
        }
      }],
      components: {
        MldContent: content
      },
      data () {
        return {
          parent: parent,
          params: params,
          saveClose: true
        }
      }
    })
    let msgBoxEl = currentMsg.$mount().$el
    document.body.appendChild(msgBoxEl)
  }
  // 弹框不加底部
  const MessageBoxInstanceNoFooter = Vue.extend(DialogNoFooter)
  let currentMsgNoFooter
  const initInstanceNoFooter = ({ content, parent, params, ok, cancel }) => {
    // 实例化vue实例
    currentMsgNoFooter = new MessageBoxInstanceNoFooter({
      mixins: [{
        methods: {
          okx (vm) {
            if (typeof ok === 'function') {
              ok(vm.$refs.mldContent)
            }
          },
          cancel (vm) {
            if (typeof cancel === 'function') {
              cancel(vm.$refs.mldContent)
            }
          }
        }
      }],
      components: {
        MldContent: content
      },
      data () {
        return {
          parent: parent,
          params: params,
          saveClose: true
        }
      }
    })
    let msgBoxElNoFooter = currentMsgNoFooter.$mount().$el
    document.body.appendChild(msgBoxElNoFooter)
  }
  // 在Vue的原型上添加实例方法，以全局调用
  Vue.prototype.$msgBox = {
    showUploadDialog (options) {
      if (!options.title) {
        options.title = '选择图片'
      }
      options.content = Upload
      this.showDialog(options)
    },
    showDialog (options) {
      if (options.showOk === false && options.showCancel === false) {
        initInstanceNoFooter({ content: options.content, parent: options.parent, params: options.params, ok: options.ok, cancel: options.cancel })
        if (typeof options === 'string') {
          // currentMsg.content = options
        } else if (typeof options === 'object') {
          Object.assign(currentMsgNoFooter, options)
        }
        currentMsgNoFooter.visible = true
        if (options.fullscreen === true) {
          currentMsgNoFooter.fullscreen = true
        } else {
          currentMsgNoFooter.fullscreen = false
        }
        if (options.width) {
          currentMsgNoFooter.width = options.width
        }
        if (options.height) {
          currentMsgNoFooter.height = options.height
        }
        currentMsgNoFooter.title = options.title
        if (options.showOk === false) {
          currentMsgNoFooter.showOk = false
        }
        if (options.showCancel === false) {
          currentMsgNoFooter.showCancel = false
        }
        if (options.minMax === false) {
          currentMsgNoFooter.minMax = false
        }
        if (options.saveClose === false) {
          currentMsgNoFooter.saveClose = false
        }
        if (options.textOk) {
          currentMsgNoFooter.textOk = options.textOk
        }
        if (options.textCancel) {
          currentMsgNoFooter.textCancel = options.textCancel
        }
        if (options.closable === false) {
          currentMsgNoFooter.closable = false
        } else {
          currentMsgNoFooter.closable = true
        }
      } else {
        // if (!currentMsg) {
        initInstance({ content: options.content, parent: options.parent, params: options.params, ok: options.ok, cancel: options.cancel })
        // }
        if (typeof options === 'string') {
          // currentMsg.content = options
        } else if (typeof options === 'object') {
          Object.assign(currentMsg, options)
        }
        currentMsg.visible = true
        if (options.fullscreen === true) {
          currentMsg.fullscreen = true
        } else {
          currentMsg.fullscreen = false
        }
        if (options.width) {
          currentMsg.width = options.width
        }
        if (options.height) {
          currentMsg.height = options.height
        }
        currentMsg.title = options.title
        if (options.showOk === false) {
          currentMsg.showOk = false
        }
        if (options.showCancel === false) {
          currentMsg.showCancel = false
        }
        if (options.minMax === false) {
          currentMsg.minMax = false
        }
        if (options.saveClose === false) {
          currentMsg.saveClose = false
        }
        if (options.textOk) {
          currentMsg.textOk = options.textOk
        }
        if (options.textCancel) {
          currentMsg.textCancel = options.textCancel
        }
        if (options.closable === false) {
          currentMsg.closable = false
        } else {
          currentMsg.closable = true
        }
      }
      // return currentMsg.showMsgBox()
    }
  }
}
export default MessageBox
